// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
        className:"DN_userView",
        Card:{
            default:null,
            type:cc.Prefab
        },
        score_altas:{
            default:null,
            type:cc.SpriteAtlas
        },
        inc_font:{
            default:null,
            type:cc.BitmapFont
        },
        des_font:{
            default:null,
            type:cc.BitmapFont
        },
        kan_frame:{
            default:null,
            type:cc.SpriteFrame,
        },
        qi_frame:{
            default:null,
            type:cc.SpriteFrame,
        },
        bai_frame:{
            default:null,
            type:cc.SpriteFrame,
        },
        Card_Anim_Space:0.05,
        Card_Anim_Time:0.5,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        this.initUI();
    },
    initUI: function(){
        this.node.thisJS = this;
        this.bgpanel = cc.find("bgpanel",this.node);  
        this.fenImg = cc.find("fenImg",this.node);
        this.score = cc.find("score",this.node);
        this.resultLabel = cc.find("resultLabel",this.node);
        this.optstate = cc.find("optstate",this.node);
        // this.fenImg.active = true ;
        // this.optstate.active = true ;
        // this.resultLabel.active = true ;
        // this.fenImg.active = true ;
        // this.resultLabel.active = true ;
     },  
    //更新头像位置
    updateUI:function(subConfig){
        this.initUI();
        for(var k in subConfig){
            console.log("k->"+k);
            this[k].setPosition(subConfig[k]);
        }
    },
    //发牌
    sendCard: function(info,noAni){
        this.bgpanel.active = true;
        var pos = this.bgpanel.convertToWorldSpaceAR(this.bgpanel.getPosition())
        var tx = this.bgpanel.getPositionX()+this.node.getPositionX();
        var ty = this.bgpanel.getPositionY()+this.node.getPositionY();
        this._cards      = {}

        var card_tmp  = cc.instantiate(this.Card);
		var size      = card_tmp.getContentSize();
        this.bgpanel.removeAllChildren();
		for(var i = 1;i<4;i++){
            global.Audio.playSFX("games/douniu/sound/effect/card_send_one.wav");
            var card_tmp  = cc.instantiate(this.Card);
            this._cards[i]=card_tmp;
            this.bgpanel.addChild(card_tmp,0);
            card_tmp.scale = 0.5;
            if(noAni){
                card_tmp.setPosition(cc.p((i-1)*28+size.width*0.5/2,size.height*0.5/2));
            }
            else{
                card_tmp.setPosition(-tx,-ty);
                var delay     = cc.delayTime(this.Card_Anim_Space*(5-i));
                var move      = cc.moveTo(this.Card_Anim_Time,cc.p(size.width*0.5/2,size.height*0.5/2));
                var delay_1ms = cc.delayTime(this.Card_Anim_Time);
                var move_line = cc.moveTo(this.Card_Anim_Time,cc.p((i-1)*28+size.width*0.5/2,size.height*0.5/2));
                var sq        = cc.sequence(delay,move,delay_1ms,move_line);

                card_tmp.runAction(sq);
            }
        }
	
    },
    showKanCard:function(info){
        this.optstate.active = true ;
        this.optstate.getComponent(cc.Sprite).spriteFrame = this.kan_frame ;
    },
    showLoseCard:function(state){
        this.optstate.getComponent(cc.Sprite).spriteFrame = this.bai_frame ;
        this.optstate.active = state ;
    },
    //用户显示扑克
    showEndCard:function(info,isQi){
        var index = 1 ;
        var self = this ;
        var tempWidth = 0 ;
        if(isQi){
            this.optstate.active = true ;
            this.optstate.getComponent(cc.Sprite).spriteFrame = this.qi_frame ;
            for(var k in this._cards){
                this._cards[k].thisJS.dark();
            }
            return ;
        }
        for(var k in info.cards){
            if(this._cards[index]){
                this._cards[index].thisJS.setValue(info.cards[k]);
                this._cards[index].thisJS.showFront();
                var card_tmp = this._cards[index] ;
                var size = card_tmp.getContentSize();
                card_tmp.setPosition(cc.p((index-1)*28+size.width*0.5/2 ,size.height*0.5/2));
            }
            index = index + 1;
        }
        // var fenImg = this.fenImg.getComponent(cc.Sprite);
        // var cardTypeFile = {
        //     0:"niu_0",1:"niu_1",2:"niu_2",3:"niu_3",
        //     4:"niu_4",5:"niu_5",6:"niu_6",7:"niu_7",
        //     8:"niu_8",9:"niu_9",10:"niu_10",11:"niu_11",
        //     12:"dn_wuhua",13:"dn_wuxiao"
        // }

       
        // fenImg.spriteFrame = this.score_altas.getSpriteFrame([cardTypeFile[info.card_type]]);
        // self.fenImg.active = true ;

        // if(info.win_money)
        // {    
        //     this.winMoneyFly(info.win_money);
        // }
    },
    //显示分数图片
    showFenImg:function(type){
        this.optstate.active = false ;
        console.log("showFenImg-->"+type);
        var cardTypeFile = {
            0:"gaopai",
            1:"duizi",
            2:"shunzi",
            3:"tonghua",
            4:"tonghuashun",
            5:"santiao",
        }
        var fenImg = this.fenImg.getComponent(cc.Sprite);
        fenImg.spriteFrame = this.score_altas.getSpriteFrame([cardTypeFile[type]]); ;
        this.fenImg.active = true ;
    },
    jiaofen:function(num){  
        this.score.getComponent(cc.Label).string = num+"分";
        this.score.active = true ;
    },

    //用户输赢金币显示
    winMoneyFly:function(win_money){
        this.resultLabel.active = true;
        var label = this.resultLabel.getComponent(cc.Label)
        
        if(win_money>=0){
		
            label.font = this.inc_font;
            label.string = "+"+win_money;
        }
        else
        {
			label.font = this.des_font;
            label.string = win_money +"";
        }
        var pos =  this.resultLabel.getPosition();
        var self = this;
        this.resultLabel.runAction(cc.sequence(cc.moveBy(0.5,cc.p(0,70)),
            cc.delayTime(1),cc.callFunc(function(){
                self.resultLabel.active = false;
                self.resultLabel.setPosition(pos);
            })
        ));
    },
    clearTable:function(){
        this._cards = {} ;
        this.bgpanel.active = false ;
        this.fenImg.active = false 
        this.score.active = false 
        this.resultLabel.active = false 
        this.optstate.active = false ;
    }

});
